﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AdminModule.CommonClasses;
using AdminModule.IndexMarkServiceReference;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace AdminModule.StructEditStratagies
{
    public class GradationsEditStrategy : StructEditBaseStrategy
    {
        private int IDTerm;

        public GradationsEditStrategy(int IDTerm)
        {
            this.IDTerm = IDTerm;
        }

        public override bool Move(DTOBase obj, DTOBase fromObject, DTOBase toObject)
        {
            if (obj is AdmGradation && fromObject is AdmGradation && toObject is RootTreeView)
            {
                AdmGradation __obj = obj as AdmGradation;
                AdmGradation __fromObject = fromObject as AdmGradation;
                RootTreeView __toObject = toObject as RootTreeView;

                __fromObject.Childs.Remove(__obj);
                __toObject.Childs.Add(__obj);

                __obj.ParentGradation = null;
                SetState(__obj, StateObject.Updated);

                return true;
            }
            if (obj is AdmGradation && fromObject is RootTreeView && toObject is AdmGradation)
            {
                AdmGradation __obj = obj as AdmGradation;
                RootTreeView __fromObject = fromObject as RootTreeView;
                AdmGradation __toObject = toObject as AdmGradation;

                __fromObject.Childs.Remove(__obj);
                __toObject.Childs.Add(__obj);

                __obj.ParentGradation = __toObject;
                SetState(__obj, StateObject.Updated);

                return true;
            }
            return false;
        }

        public override bool Delete(DTOBase obj, DTOBase fromObject)
        {
            if (obj is AdmGradation)
            {
                AdmGradation __obj = obj as AdmGradation;
                DTOChilds __fromObject = fromObject as DTOChilds;

                __fromObject.Childs.Remove(__obj);

                SetState(__obj, StateObject.Deleted);

                return true;
            }
            return false;
        }

        public override bool Add(DTOBase obj, DTOBase toObject)
        {
            if (obj is AdmGradation && toObject is RootTreeView)
            {
                AdmGradation __obj = obj as AdmGradation;
                RootTreeView __toObject = toObject as RootTreeView;

                __toObject.Childs.AddToForward(__obj);

                __obj.ParentGradation = null;
                __obj.IDTerm = this.IDTerm;

                SetState(__obj, StateObject.Created);

                return true;
            }
            if (obj is AdmGradation && toObject is AdmGradation)
            {
                AdmGradation gradation = obj as AdmGradation;
                AdmGradation parentGradation = toObject as AdmGradation;

                // Если добавляется сразу группа
                if (gradation.IDGradationTerm != 0 && gradation.IDParentGradationTerm == null)
                {
                    // Исключаем из градации неотмеченные дочерние элементы элементы
                    //gradation.ExcludeUnchekedItems();

                    foreach (AdmGradation gr in gradation.Childs)
                    {
                        gr.ParentGradation = parentGradation;
                        parentGradation.Childs.Add(gr);

                        SetState(gr, StateObject.Created, new List<Type> { typeof(AdmGradation) });
                    }
                }
                else
                {
                    gradation.IDTerm = this.IDTerm;
                    gradation.ParentGradation = parentGradation;
                    parentGradation.Childs.AddToForward(gradation);

                    SetState(gradation, StateObject.Created);

                    return true;
                }
            }
            return false;
        }

        public override DTOBase CopyTo(DTOBase obj, DTOBase toObject, int toIDTerm)
        {
            return base.CopyTo(obj, toObject, toIDTerm);
        }

        public override void Replace(DTOBase new_obj, DTOBase old_obj, DTOBase parentObject)
        {
            if (new_obj is AdmGradation && old_obj is AdmGradation && parentObject is DTOChilds)
            {
                AdmGradation new_gradation = new_obj as AdmGradation;
                AdmGradation old_gradation = old_obj as AdmGradation;

                old_gradation.CopyFrom(new_gradation);
            }
        }
    }
}
